package cc.rengu.igas.channel.allinpay.core.packswitch;

import cc.rengu.igas.channel.allinpay.common.enums.RespCodeEnum;
import cc.rengu.jradp.packetswitch.IncomingPackSwitchImpl;
import cc.rengu.jradp.packetswitch.OutgoingPackSwitchImpl;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.CertInfoMapper;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.dao.SrcChannelInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.CertInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SecPlanInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SrcChannelInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.CertInfo;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.service.common.entity.SrcChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
//import com.allinpay.XmlTools;

/**
 * @version 1.0.0
 * @date 2021/03/21
 */

public class AllinpayClientPackSwitch implements IncomingPackSwitchImpl, OutgoingPackSwitchImpl {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public byte[] beforeInComPackSwitch(byte[] bytes) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String recvData = new String(bytes, "GBK");
        rglog.debug("recvData = <{}>", recvData);
        /*获取公钥*/
        //获取通道信息
        SrcChannelInfoMapper srcChannelInfoMapper = new SrcChannelInfoMapperImpl();
        SrcChannelInfo srcChannelInfo = srcChannelInfoMapper.selectSrcChannelInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), "ALLINPAY", "*");
        SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();
        SecPlanInfo secPlanInfo = secPlanInfoMapper.selectSecPlanInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), srcChannelInfo.getSecPlanId(), "CD00");
        if (null == secPlanInfo) {
            rglog.error("安全计划信息为配置secPlanId:<{}>,secPlanType:<{}>", srcChannelInfo.getSecPlanId(), "CD00");
            throw new BizException(RespCodeEnum.CERT_LOAD_INVALID.getRespCode(), RespCodeEnum.CERT_LOAD_INVALID.getRespDesc());
        }
        //获取证书路径
        CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
        CertInfo certInfo = certInfoMapper.selectCertInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID) + srcChannelInfo.getSecPlanId() + "CD00");
        if (null == certInfo) {
            throw new BizException(RespCodeEnum.CERT_LOAD_INVALID.getRespCode(), RespCodeEnum.CERT_LOAD_INVALID.getRespDesc());
        }
//        boolean flag = XmlTools.verifySign(recvData, certInfo.getCertSavePath(), false, false);
//        if (flag){
//            rglog.info("验签成功");
//        }else {
//            rglog.error("验签失败");
//        }
        return recvData.getBytes("GBK");
    }

    @Override
    public int afterInComPackSwitch() throws Exception {
        return 0;
    }

    @Override
    public int beforeOutgoPackSwitch() throws Exception {
        return 0;
    }

    @Override
    public byte[] afterOutgoPackSwitch(byte[] bytes) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
//        XmlTools xmlTools = new XmlTools();
        String sendData = new String(bytes, "GBK");
        /*String sendData = "<?xml version='1.0' encoding=\"GB2312\"?>\n" +
                "<AIPG><INFO><TRX_CODE>211011</TRX_CODE><VERSION>05</VERSION><DATA_TYPE>2</DATA_TYPE><LEVEL>5</LEVEL><USER_NAME>20060400001069704</USER_NAME><USER_PASS>111111</USER_PASS><REQ_SN>2006040000106972021041980183905</REQ_SN><SIGNED_MSG></SIGNED_MSG></INFO><VALIDTR><MERCHANT_ID>200604000010697</MERCHANT_ID><SUBMIT_TIME>20210419151135</SUBMIT_TIME><BANK_CODE>307</BANK_CODE><ACCOUNT_ATTRB>1</ACCOUNT_ATTRB><ACCOUNT_NO>6216261000000000018</ACCOUNT_NO><ACCOUNT_NAME>全渠道</ACCOUNT_NAME><ID_TYPE>0</ID_TYPE><ID>341126197709218366</ID><TEL>13552535506</TEL><MERREM>齐商银行</MERREM><REMARK></REMARK></VALIDTR></AIPG>";*/
        rglog.debug("未签名的报文=<{}>", sendData);
        //获取通道信息
        SrcChannelInfoMapper srcChannelInfoMapper = new SrcChannelInfoMapperImpl();
        SrcChannelInfo srcChannelInfo = srcChannelInfoMapper.selectSrcChannelInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), "ALLINPAY", "*");
        SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();
        SecPlanInfo secPlanInfo = secPlanInfoMapper.selectSecPlanInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID), srcChannelInfo.getSecPlanId(), "CE10");
        if (null == secPlanInfo) {
            rglog.error("安全计划信息为配置secPlanId:<{}>,secPlanType:<{}>", srcChannelInfo.getSecPlanId(), "CE10");
            throw new BizException(RespCodeEnum.CERT_LOAD_INVALID.getRespCode(), RespCodeEnum.CERT_LOAD_INVALID.getRespDesc());
        }
        //获取证书路径
        CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
        CertInfo certInfo = certInfoMapper.selectCertInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID) + srcChannelInfo.getSecPlanId() + "CE10");
        if (null == certInfo) {
            throw new BizException(RespCodeEnum.CERT_LOAD_INVALID.getRespCode(), RespCodeEnum.CERT_LOAD_INVALID.getRespDesc());
        }
        String sendXml = null;
        rglog.debug("证书路径=<{}>,证书密码=<{}>,签名前报文=<{}>", certInfo.getCertSavePath(), certInfo.getCertPassword(), sendData);
        try {
//             sendXml = xmlTools.signMsg(sendData, certInfo.getCertSavePath(), certInfo.getCertPassword(), false);
        } catch (Exception e) {
            rglog.debug("xmlTools:<{}>", e.getMessage());
            throw new BizException(RespCodeEnum.SIGN_ERROR.getRespCode(), RespCodeEnum.SIGN_ERROR.getRespDesc());
        }
        rglog.debug("签名后报文=<{}>", sendXml);
        xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/ArgList", "Content-Type");
        xmlTreeUtil.setXmlTreeStringValue("_MESSAGEHEAD/http/Content-Type", "application/xml;charset=GBK");
        return sendXml.getBytes("GBK");
    }
}
